package reflect15.classdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

class Person
{
	private String name;
	private int age;
	public Person()
	{
		
	}
	public Person(String name,int age)
	{
		this.setName(name);
		this.setAge(age);
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getAge()
	{
		return age;
	}
	public void setAge(int age)
	{
		this.age = age;
	}
	
	public String toString()
	{
		return "姓名："+this.getName()+",年龄："+this.getAge();
	}
}

public class ClassDemo01
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Class<?> c1 = null;
		Class<?> c2 = null;
		Class<?> c3 = null;
		c1 = Person.class;
		Constructor<?> [] cons = null;
		
		try
		{
			c2 = Class.forName("ClassDemo.Person");
		}
		catch(ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		c3 = new Person().getClass();
		cons = c3.getConstructors();
		
		Person per = null;
		Person per1 = null;
		try
		{
			per1 = (Person)cons[1].newInstance("liuzq",40);
		}
		catch(IllegalArgumentException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch(InstantiationException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch(IllegalAccessException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch(InvocationTargetException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try
		{
			if(c1.newInstance() instanceof Person)
			{
				per = (Person)(c1.newInstance());
			}
		}
		catch(InstantiationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch(IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		per.setAge(30);
		per.setName("刘溯海");
		System.out.println(per.toString());
		System.out.println(per1.toString());
		System.out.println(c1.getName());
		System.out.println(c1.getName());
		System.out.println(c1.getName());
		
	}

}
